package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.AnnexService;
import com.iwop.moto.service.OrderGoodsService;
import com.iwop.moto.service.OrderRefundService;
import com.iwop.moto.utils.api.Kdniao.KdniaoTrackQueryAPI;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * liuwb 2016-07-27 15:31
 */
@Controller
public class OrderGoodsController extends  BaseController {

    @Autowired
    private OrderGoodsService orderGoodsService;

    @Autowired
    private OrderRefundService orderRefundService;

    @Autowired
    AnnexService annexService;
    /**
     **************************************
     * 功能:单商品 - 新增
     * liuwb 2016-07-27 16:34
     * 参数: userId 买家Id
     * 参数: storeId 店铺Id
     * 参数: gatherName 收货人姓名
     * 参数: gatherTel 收货人手机号
     * 参数: gatherAddress 收货人地址
     * 参数: orderDetailJson json字符串
     ***************************************
     * 【orderDetailJson】json 字段
     * 参数:goodsId 商品ID
     * 参数:goodname 商品名称
     * 参数:imgpath 商品缩略图
     * 参数:model 商品型号
     * 参数:colors 颜色
     * 参数:price 单价
     * 参数:nums 数量
     * 参数:goods_detail_id 商品规格主键
     ***************************************
     */
    @RequestMapping("/api/order/goods/add")
    @ResponseBody
    public ResultModel apiOrderGoodsAdd(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String orderNo = SequenceUtil.Serial();
            SRModel.put("orderNo",orderNo);
            SRModel.put("orderTime",new Date());//订单下单时间
            SRModel.put("payStatus","0");//交易状态[0:未付款][1:已付款][2:同意退款][3:拒绝退款]
            SRModel.put("payTime",new Date());//交易状态时间
            SRModel.put("orderStatus","0");//订单状态[0:未发货][1:已发货][2:已收货][3:已退货]
            SRModel.put("orderStatusTime",new Date());//订单状态时间
            SRModel.put("delete_flag","1");
            orderGoodsService.insertSelective(SRModel);
            String pictrues = SRModel.getString("pictrues");
            if(!StringUtils.isEmpty(pictrues)){
                String[] split = pictrues.split(",");
                for (String path:split){
                    SuperRecordModel sr=new SuperRecordModel();
                    sr.put("aid", UUID.randomUUID().toString());
                    sr.put("types","Goods");
                    sr.put("pid",orderNo);
                    sr.put("createdate",new Date());
                    annexService.insertAnnex(sr);
                }
            }
            List<SuperRecordModel> list =  SRModel.jsonToList(SRModel.getString("orderDetailJson"));
            for(SuperRecordModel ls : list){
                ls.put("sid",UUID.randomUUID().toString());
                ls.put("orderNo",orderNo);
                orderGoodsService.insertDetailSelective(ls);
            }

            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:商品订单 - 根据订单状态获取门店的订单
     * liuwb 2016-07-27 19:30
     * 参数: storeId 门店id
     * 参数: payStatus[交易状态] or orderStatus[订单状态]
     * <!-- 交易状态[0:未付款][1:已付款][2:同意退款][3:拒绝退款][4:退款中] -->
     * <!-- 订单状态[0:未发货][1:已发货][2:已收货][3:已退货][4:待评价][5:已评价] -->
     */
    @RequestMapping("/api/order/goods/list")
    @ResponseBody
    public ResultModel apiOrderGoodsList(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> lists = orderGoodsService.findOrderGoodsByStoreId(SRModel);
            for(SuperRecordModel ls : lists){
                ls.put("orderDetailJson",orderGoodsService.findDetailByOrderNo(ls));
            }
            return getSucceed(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:商品订单 - 订单详情
     * liuwb 2016-07-27 19:30
     * 参数: order_no 订单号
     */
    @RequestMapping("/api/order/goods/detail")
    @ResponseBody
    public ResultModel apiOrderGoodsDetail(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel = orderGoodsService.findByOrderNo(SRModel);
            SRModel.put("orderDetailJson",orderGoodsService.findDetailByOrderNo(SRModel));
            SuperRecordModel order_no = orderRefundService.getOrderRefund(SRModel.getString("order_no"));
            SRModel.put("orderRefunddetail",order_no);
            return getSucceed(SRModel);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 商品订单 - 根据订单号修改交易状态
     * liuwb 2016-07-27 19:49
     * orderNo :订单号
     * payStatus :交易状态[0:未付款][1:已付款][2:同意退款][3:拒绝退款][4:退款中][5:交易关闭][6:交易成功]
     */
    @RequestMapping("/api/order/goods/paystatus")
    @ResponseBody
    public ResultModel apiOrderPayStatus(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("payTime",new Date());
            if(SRModel.getString("orderStatus").equals("1")){
                SRModel.put("order_success_time",new Date());
            }
            orderGoodsService.upOrderPayStatus(SRModel);
            if(SRModel.getString("orderStatus").equals("4")){
                String sid = UUID.randomUUID().toString();
                SRModel.put("sid", sid);
                SRModel.put("apply_time",new Date());
                SRModel.put("refund_status","0");
                orderRefundService.inserTorderRefund(SRModel);
            }
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * 商品订单 - 根据订单号修改订单状态
     * liuwb 2016-07-27 19:50
     * orderNo :订单号
     * orderStatus  :订单状态[0:未发货][1:已发货][2:已收货][3:已退货][4:待评价][5:已评价]
     * express_company： 公司sid
     * express_no：运单号
     */
    @RequestMapping("/api/order/goods/status")
    @ResponseBody
    public ResultModel apiOrderStatus(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("orderStatusTime",new Date());
            if (SRModel.getString("orderStatus").equals("1")){
                SRModel.put("order_send_time",new Date());
            }
            orderGoodsService.upOrderStatus(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * 查询快递单号
     * expCode 快递公司简称
     * expNo   快递单号(运单号)
     * @return json
     */
    @RequestMapping("/api/express/search")
    @ResponseBody
    public ResultModel ApiExpressSearch(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String exp = KdniaoTrackQueryAPI.getOrderTracesByJson(SRModel.getString("expCode"),SRModel.getString("expNo"));
            SRModel.putAll(SRModel.toMap(exp));
            return getSucceed(SRModel);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 删除订单
     * expCode 快递公司简称
     * expNo   快递单号(运单号)
     * @return json
     */
    @RequestMapping("/api/order/delete")
    @ResponseBody
    public ResultModel orderDelete(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SuperRecordModel byOrderNo = orderGoodsService.findByOrderNo(SRModel);
            String pay_status = byOrderNo.getString("pay_status");
            if(pay_status.equals("5")){
                SRModel.put("delete_flag","0");
                SRModel.put("delete_time",new Date());
                orderGoodsService.upOrderGoodsDelete(SRModel);
                return getSucceed(SRModel);
            }else{
                return getErr(ErrCode.ERROR_SYSTEM);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

}
